<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2023/6/17
 * Time: 15:19
 */
namespace app\admin\controller;

use app\middleware\AdminCheck;
use think\facade\Db;

class Brand {

    protected $middleware = [
        AdminCheck::class => ['except' 	=> [] ]
    ];

    /**
     * 品牌列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function store(){
        $data = validateParam(['brand_first_letter', 'brand_name'], ['brand_first_letter', 'brand_name']);
        $data['brand_first_letter'] = strtoupper($data['brand_first_letter']);
        if (!isset($data['id']) || empty($data['id'])) {
            Db::name('brands')->insert($data);
        } else {
            Db::name('brands')->where('id', input('id'))->update($data);
        }

        return success('保存成功');
    }

    /**
     * 品牌列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(){
        $param = input();
        $where = ['is_delete' => 0];
        if(isset($param['is_hot']) && $param['is_hot'] !== ''){
            $where[] = ['is_hot','=',$param['is_hot']];
        }
        if(isset($param['brand_name']) && $param['brand_name'] !== ''){
            $where[] = ['brand_name','=',$param['brand_name']];
        }
        $list = Db::name('brands')
            ->field(['id', 'brand_first_letter', 'brand_name'])
            ->where($where)
            ->select();

        return success('品牌列表', ['list'=>$list]);
    }

    /**
     * 设置和取消热门
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setHot(){
        validateParam('id','id');
        //首先判断现在是否是热门
        $city = Db::name('brands')
            ->where('id',input('id'))
            ->find();
        if($city['is_hot'] === 1){
            //说明是热门
            Db::name('brands')
                ->where('id',input('id'))
                ->update(['is_hot'=>0]);
            return success('取消热门');
        }else{
            //不是热门
            Db::name('brands')
                ->where('id',input('id'))
                ->update(['is_hot'=>1]);
            return success('设置热门');
        }
    }


    /**
     *  删除品牌
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(){
        validateParam('id','id');
        Db::name('brands')
            ->where('id',input('id'))
            ->update(['is_delete'=>1]);
        return success('设置成功');
    }
}